<?php

require('config.php');

if(!isset($_SESSION['id_user']))
    redirectFront('connexion');

$page = new rootLayoutPage();
$page->setBaliseTitle('profil_pass_balise');
$page->setDescription('profil_pass_description');

$page->addJavaScript('jquery.validate.js');

//-- menu Left show products--------------------------------//
$menu = new layoutMenuLeft();
$menuCategorie = new layoutMenuPage();
$menu->pushMenu($menuCategorie);
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

$translate = tmxTranslate('profil-pass.xml', REP_XLAT_TEMPLATE);
if(isset($_POST['submit'])) {
    try {
        $timestamp = $_SESSION['profil_pass_timestamp'];
        session_unregister('profil_pass_timestamp');
        $apass = trim($_POST['apass_'.$timestamp]);
        $pass  = trim($_POST['pass_'.$timestamp]);
        $cpass = trim($_POST['cpass_'.$timestamp]);

        if($apass=='' or $pass=='' or $cpass=='')
            throw new Exception($translate['message_1']);

        if(!modelUser::verifPass($apass))
            throw new Exception($translate['message_1']);

        if($pass != $cpass)
            throw new Exception($translate['message_3']);

        modelUser::updatePass($_SESSION['id_user'], $pass);
        $msg = new Message($translate['message_2'], Message::INFO);
        Persistence::register(PERSIST_MESSAGE, $msg);
        redirectFront('profil');
    }
    catch(Exception $e) {
        $msg = new Message($e->getMessage(), Message::AVERT);
        Persistence::register(PERSIST_MESSAGE, $msg);
    }
}

//-- corps de la page -----------//
$tpl_file='profil-pass.tpl';
$profil_pass = new flyLayout(REP_TPL . $tpl_file);
$profil_pass->start();

//$tabs = new layoutTabs();
//$tabs->pushLink('Mon compte', '/profil.php');
//$tabs->pushLink('Mes messages', '/profil-message.php');
//$profil_pass->includeLayout('tabs', $tabs);

$_SESSION['profil_pass_timestamp'] = time();
$profil_pass->setVariable('timestamp', $_SESSION['profil_pass_timestamp']);
$profil_pass->stop();
$page->setBody($profil_pass);

$page->display();

?>